package se.kth.fericss.rts;

import java.awt.Dimension;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;

import se.kth.fericss.obj.*;

public class World {
	private Game game;
	private Gui gui;
	private Dimension worldSize;
	private List<GameObject> gameObjects;
	private List<GameObject> objectsToAdd;
	private Random rand = new Random();
	private Team playerTeam = new Team("Player 1", Color.red);
	private Ai ai;

	public World(Game game, Dimension worldSize) {
		this.game = game;
		this.worldSize = worldSize;
		gameObjects = new ArrayList<GameObject>();
		objectsToAdd = new ArrayList<GameObject>();
		ai = new Ai(this);
	}
	public Dimension getWorldSize() {
		return new Dimension(worldSize);
	}

	public int getWidth() {
		return worldSize.width;
	}

	public int getHeight() {
		return worldSize.height;
	}

	public Team getPlayerTeam() {
		return playerTeam;
	}

	public Team getAiTeam() {
		return ai.getTeam();
	}
	
	public void update(int delta) {
		//Timer.updateTimers(delta);
		
		//Reset collision data
		//for(GameObject object : gameObjects) {
		//	object.setColliding(false);
		//}

		//Add the objects created last update
		for(GameObject objectToAdd : objectsToAdd) {
			gameObjects.add(objectToAdd);
		}
		objectsToAdd.clear();

		//ai.update();
		
		//Update state for all GameObjects
		//for(GameObject object : gameObjects) {
		//	object.update();

			//All the player's hunters shoot arrows at the nearest enemy while the Z key is held down
		//	if(object instanceof Hunter && game.getInput().isKeyDown(Input.KEY_Z)) { //TODO shouldn't check input here
		//		Hunter hunter = (Hunter)object;
		//		ControllableObject enemy = hunter.getClosest(ControllableObject.class, false);
		//		if(enemy != null && hunter.getTeam() == playerTeam) {
		//			hunter.shootArrowAt(enemy);
		//		}
		//	}
		//}
		//Remove dead objects from the game
		//Iterator<GameObject> it = gameObjects.iterator();
		//while(it.hasNext()) {
			//GameObject object = it.next();
			//if(!object.isAlive()) {
			//	object.destroy();
			//	it.remove();
			//}
		//}
	}
	public void init(Gui gui) {
		this.gui = gui;
		
		//Create a village center with a stockpile with some resources and add it to the game
		//Building testCenter = new VillageCenter(this, new Point(300, 300), playerTeam);
		//Stockpile testResourcePile = new Stockpile(this, testCenter);
		//testCenter.completed();
		//testResourcePile.addResource(Resource.WOOD, 10);
		//testResourcePile.addResource(Resource.STONE, 10);
		//playerTeam.addResourcePile(testResourcePile);
		//testCenter.setStockpile(testResourcePile);
		//addObject(testCenter);
		//addObject(testResourcePile);
		
		//placeUnitsRandomly(100, playerTeam);
	}
	public List<GameObject> getAllUnits() {
		// TODO Auto-generated method stub
		return null;
	}
	public void render(Graphics g) {
		for(GameObject object : gameObjects) {
			object.draw(g);
		}
	}

	public void addObject(GameObject object) {
		objectsToAdd.add(object);
	}
}
